<?php

namespace App\Http\Imports\Storage;

use App\Models\Company;
use App\Models\Dic;
use App\Models\Material;
use App\Models\MpcLocation;
use App\Models\MpcLot;
use App\Models\ProColor;
use App\Models\Type;
use App\Repositories\MpcOtherRepository;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

/**
 * Summary of OtherImport
 * @property MpcOtherRepository $repository
 * @author sunkaiyuan
 */
class OtherImport implements ToCollection
    {
    /**
     * @param Collection $collection
     */
    public function __construct(MpcOtherRepository $re)
        {
        $this->repository = $re;
        }
    public function collection(Collection $collection)
        {
        set_time_limit(0);
        $rows = $collection->map(function ($v, $k)
            {
            if ($k == 0) {
                return false;
                }
            $billdate           = $v['0'];
            $company_code       = $v['1'];
            $location_name      = $v['3'];
            $material_code      = $v['4'];
            $type_name          = $v['6'];
            $dyelot             = $v['7'];
            $color_name         = $v['8'];
            $auxiliary_quantity = $v['9'];
            $quantity   = $v['10'];
            $unit_name  = $v['11'];
            $reel       = $v['12'];
            $price      = $v['13'];
            $iscustomer = $v['15'];
            $remark    = $v['16'];
            $grade     = $v['17'];
            $craft     = $v['18'];
            $sample    = $v['19'];
            $orderno   = $v['20'];
            $cust_no   = $v['21'] ?? "";
            $company_m = null;
            if (isNotEmpty($company_code)) {
                $company_m = Company::whereCode($company_code)->first();
                if ($company_m == null) {
                    fail("第" . $k . "行上传失败！{$company_code}往来单位未找到！");
                    }
                }

            $location_m = MpcLocation::where("name", $location_name)->first();
            if ($location_m == null) {
                fail("第" . $k . "行上传失败！{$location_name}库位未找到！");
                }

            $material_m = Material::whereCode($material_code)->first();
            if ($material_m == null) {
                fail("第" . $k . "行上传失败！{$material_code}物料未找到！");
                }
            $type_m = Type::whereName($type_name)->first();
            if ($type_m == null) {
                fail("第" . $k . "行上传失败！{$type_name}形态未找到！");
                }
            $unit_m = Dic::whereCode("unit")->whereName($unit_name)->first();
            if ($unit_m == null) {
                fail("第" . $k . "行上传失败！{$unit_name}单位未找到！");
                }

            if (!in_array(trim($iscustomer), ['是', '否'])) {
                fail("第" . $k . "行上传失败！是否客户物料设置不正确！必须是[是/否]");
                }
            return [
                'billdate'           => $billdate,
                'company_id'         => $company_m?->id ?? 0,
                'material_id'        => $material_m->id,
                'location_id'        => $location_m->id,
                'type_id'            => $type_m->id,
                'unit_id'            => $unit_m->key,
                'iscustomer'         => trim($iscustomer) == '是' ? 1 : 0,
                'customer_id'        => trim($iscustomer) == '是' ? $company_m->id : 0,
                'dyelot'             => $dyelot,
                'color_name'         => $color_name,
                'auxiliary_quantity' => $auxiliary_quantity,
                'quantity'           => $quantity,
                'reel'               => $reel,
                'price'              => $price,
                'amount'             => $price * $quantity,
                'remark'             => $remark,
                'grade'              => $grade ?? "",
                'craft'              => $craft ?? "",
                'sample_data'        => $sample ?? "",
                'orderno'            => $orderno ?? "",
                'custno'             => $cust_no ?? ""
            ];
            })->filter(fn($v) => $v);
        $this->save($rows);
        }


    protected function save($ReqData)
        {

        $main['quantity']           = 0;
        $main['auxiliary_quantity'] = 0;
        $main["details"]            = array_filter(
            $ReqData->toArray(),
            function ($row) use (&$main)
                {
                $main['quantity'] += $row['quantity'];
                $main['auxiliary_quantity'] += $row['auxiliary_quantity'];
                $main['billdate'] = $row['billdate'];
                return $row;
                }
        );

        $gdata = $this->repository->autosave($main);
        $key   = 1;
        $gdata->details->filter(function ($v) use ($gdata, &$key)
            {
            $v->batchno = $gdata->code . substr("00" . ($key++), -2, 2);
            $v->save();
            });
        }
    }
